{include file="common/comm_header" /}

<style>
    .layui-common-width{
        width: 50%;
    }
    .layui-hidden{
     display: none;
    }
    .layui-pre{
        height: 200px;
        overflow:auto;
        overflow-x:hidden;
        margin: 0;
        padding: 0;
    }
</style>
<body>
<div class="x-body layui-anim layui-anim-up">
    <div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">网站管理</a>
        <a href="">批量导入</a>
      </span>
        <a class="layui-btn layui-btn-lg" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:window.location.reload();" title="刷新">
            <i class="layui-icon" style="line-height:40px">刷新</i></a>
    </div>
    <div  style="padding-left: 20%">
        <div class="layui-form-item">
            <div class="layui-form-mid layui-word-aux">
                <b>仅支持excel文件，数据最大支持1M</b>
                <br/>
                <b>文件表头格式为：分类标题、网址、网站标题</b>
            </div>
            <div style="line-height: 55px;" class="layui-input-inline">
                <input type="file" onchange="importf(this)" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" name="batch_file" id="batch_file" class="layui-btn">
            </div>
        </div>

        <div class="pre layui-collapse layui-common-width layui-hidden">
            <h2 id="pre_title" class="layui-colla-title"></h2>
            <pre id="pre" class="layui-pre"></pre>
        </div>

        <div class="layui-form-item" style="padding-top: 20px;">
            <label for="L_tj" style="padding-left: 120px;" class="layui-form-label">
            </label>
            <div class="layui-input-block">
                <button id="L_tj" disabled="disabled" class="layui-btn  layui-btn-normal  layui-btn-disabled">点击上传</button>
            </div>
        </div>
    </div>


</div>
<script type="text/javascript" src="/static/common/js/jquery.min.js"></script>
<script type="text/javascript" src="/static/common/js/xlsx.full.min.js" charset="utf-8"></script>
<script type="text/javascript" src="/static/admin//lib/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript" src="/static/admin//js/xadmin.js"></script>
<!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
<!--[if lt IE 9]>
<script src="/static/common/js/html5.min.js"></script>
<script src="/static/common/js/respond.min.js"></script>
<![endif]-->
<script>
    layui.use(['element','code'], function(){
        var element = layui.element;
        layui.code();
    });

    ///自定义错误
    onerror=handleErr;
    function handleErr(msg,url,l)
    {
        var txt="";
        txt+="请检查选择的文件，页面错误信息: " + msg + "\n";
        layer.alert(txt, {
            icon: 5,
            time: 2000
        });
        return true
    }

    /*
     FileReader共有4种读取方法：
     1.readAsArrayBuffer(file)：将文件读取为ArrayBuffer。
     2.readAsBinaryString(file)：将文件读取为二进制字符串
     3.readAsDataURL(file)：将文件读取为Data URL
     4.readAsText(file, [encoding])：将文件读取为文本，encoding缺省值为'UTF-8'
     */
    var fs = 1;
    var last_web = '';
    var last_len = 0;
    //读取完成的数据
    var wb;
    //是否将文件读取为二进制字符串
    var rABS = false;
    //文件流转BinaryString
    function fixdata(data) {
        var o = "",
                l = 0,
                w = 10240;
        for(; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)));
        o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
        return o;
    }
    ///获取json数据长度
    function getJsonLength(jsonData) {
        var length = 0;
        for(var ever in jsonData) {
            length++;
        }
        return length;
    }
    ///读取文件内容方法
    function importf(obj) {

        ///获得文件对象
        var pic = obj.files[0];
        if(typeof(pic)=="undefined"){
            return false;
        }
        ///判断文件类型
        var type = pic.type;
        if(type!="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"&&type!="application/vnd.ms-excel"){
            layer.alert('只能上传excel文件啊！', {
                icon: 5,
                time: 2000
            });
            return false;
        }
        ///判断文件大小
        var size=parseInt(pic.size)/1024/1024;             //初始文件大小是字节，换算成M
        if(size>1){
            layer.alert('文件大小超过1M了！', {
                icon: 5,
                time: 2000
            });
            return false;
        }

        ///读取文件数据
        var reader = new FileReader();
        if(rABS) {
            reader.readAsArrayBuffer(pic);
        } else {
            reader.readAsBinaryString(pic);
        }
        ///
        reader.onload = function(e) {
            var data = e.target.result;
            if(rABS) {
                wb = XLSX.read(btoa(fixdata(data)), {//手动转化
                    type: 'base64'
                });
            } else {
                wb = XLSX.read(data, {
                    type: 'binary'
                });
            }

            //wb.SheetNames[0]是获取Sheets中第一个Sheet的名字
            //wb.Sheets[Sheet名]获取第一个Sheet的数据
            //document.getElementById("demo").innerHTML= ;
            var json_data = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
            var stringify_json_data = JSON.stringify(json_data, null, 2);
            var json_data_length = getJsonLength(json_data);
            document.getElementById("pre").innerHTML= stringify_json_data;
            document.getElementById("pre_title").innerHTML= '<h1>除去表头一共 <b>'+json_data_length+'</b> 条数据</h1>';

            last_len = json_data_length;
            last_web = json_data;
            fs = 2;
            $(".pre").removeClass("layui-hidden");
            $("#pre").addClass("layui-code");
            $("#L_tj").attr('disabled',false).removeClass('layui-btn-disabled');
        };
    }


    $('#L_tj').bind('click',function(){
        if(fs != 2 || last_len == 0){
            window.location.reload();
            return false;
        }

        $("#L_tj").attr('disabled',true);
        $.ajax({
            url:'/{$Think.ADMIN_ROUT_PREFIX}/web-batch/'+Math.random(),
            timeout : 5000,
            type : 'post',
            data : {batch_data:JSON.stringify(last_web)},
            async: true,
            dataType:'json',
            success:function(data){
                var testJson = $.parseJSON(data);
                if(testJson.status == 0){
                    layer.alert(testJson.msg, {
                        icon: 6,
                        time: 8000,
                        closeBtn:0,
                        end:function () {
                            window.location.href = testJson.url;
                        }
                    });
                }else if(testJson.status == 2){
                    layer.alert(testJson.msg, {
                        icon: 5,
                        time: 2000
                    });
                    $("#L_tj").attr('disabled',false);
                    return false;
                }else{
                    layer.alert('出现异常', {
                        icon: 5,
                        time: 2000
                    });
                    $("#L_tj").attr('disabled',false);
                    return false;
                }
            },
            error:function(){
                layer.alert('出现异常，建议刷新页面。', {
                    icon: 5,
                    time: 2000
                });
                $("#L_tj").attr('disabled',false);
                return false;
            }
        });

        return false;
    });



</script>
</body>

{include file="common/comm_bottom" /}